<template>
  <view class="login" v-if="visible">
    <view class="image">
      <uni-icons color="#FF0719" type="contact" size="120"></uni-icons>
      <view class="text">线上应流必备工具</view>
    </view>

    <!-- <button class="btn" @tap="login">微信登录</button> -->
    <!-- <button open-type="getUserInfo" @tap="getUserInfo()">获取头像昵称</button> -->
    <button open-type="getUserInfo" @tap="getUserInfo">获取头像昵称</button>
  </view>
</template>
<script setup lang="ts">
const userInfoStore = useUserInfoStore();
let visible = ref(false);
let $emit = defineEmits(["loginSuccess"]);
/**
 * 验证是否登录
 */
function checkLogin() {
  if (!userInfoStore.openId) {
    visible.value = true;
  } else {
    $emit("loginSuccess");
  }
}

function getUserInfo() {
  wx.getUserProfile({
    desc: "用于完善用户信息", // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    success: res => {
      console.log("获取用户信息成功", res);
      const params = {
        nick: res.userInfo.nickName,
        headUrl: res.userInfo.avatarUrl,
        openId: userInfoStore.openId
      };
      const storageUserInfo = uni.getStorageSync(CONSTANT_PARAMS.USER_INFO);
      Object.assign(storageUserInfo, {
        nickName: res.userInfo.nickName,
        avatarUrl: res.userInfo.avatarUrl
      });
      uni.setStorageSync(CONSTANT_PARAMS.USER_INFO, storageUserInfo);
      // userInfo.nickName, userInfo.avatarUrl
      userInfoStore.setUserInfo(res.userInfo.nickName, res.userInfo.avatarUrl);
      userWxUpdateInfo(params).then(() => {
        visible.value = false;
        $emit("loginSuccess");
      });
    }
  });
}

defineExpose({
  checkLogin
});
</script>
<style lang="scss" src="./index.scss" scoped></style>
